# -*- codeing = utf-8 -*-
#@Time : 2021/7/24 0024 上午 0:33
#@Author : baiguanghui
#@File : views.py
#@Software : PyCharm
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
POST_FORM = '''
<form method='post' action='/test_get_post'>
    用户名:<input type='text' name='uname'>
    <input type='submit' value='提交'>
</form>
'''


def page_2003_view(request):
    html = "<h1>这是第一个页面</h1>"
    return HttpResponse(html)
def index_view(request):
    html = '这是我的首页'
    return HttpResponse(html)
def page1_view(request):
    html = '这是编号为1的页面'
    return HttpResponse(html)
def page2_view(request):
    html = '这是编号为2的页面'
    return HttpResponse(html)
def pagen_view(request,pg):
    html = '这是编号为%s的网页!'%(pg)
    return HttpResponse(html)
def cal_view(request,n,op,m):
    if op not in ['add','sub','mul']:
        return HttpResponse('your  op  is  wrong')
    result = 0
    if op == 'add':
        result = n + m
    elif op == 'sub':
        result = n - m
    elif op == 'mul':
        result = n * m
    return HttpResponse('结果为：%s'%(result))
def cal2_view(request,x,op,y):
    html = 'x:%s  op:%s  y:%s'%(x,op,y)
    return HttpResponse(html)
def birthday_view(request,y,m,d):
    html = '生日为%s年%s月%s日'%(y,m,d)
    return HttpResponse(html)

def test_request(request):
    print('path info is',request.path_info)
    print('method is',request.method)
    print('querystring is',request.GET)
    print('full path is',request.get_full_path())

    # return HttpResponse('test request ok')
    return HttpResponseRedirect('page/1')
def test_get_post(request):
    if request.method == 'GET':
        print(request.GET)
        #测试地址：http://127.0.0.1:8000/test_get_post?a=400
        print(request.GET['a'])
        print(request.GET.get('c','no c'))
        # 测试地址：http://127.0.0.1:8000/test_get_post?a=100&a=200&a=400
        # 问卷调查  form  GET 兴趣爱好，复选框
        print(request.GET.getlist('a'))

        return HttpResponse(POST_FORM)
    elif request.method == 'POST':
         # 处理用户提交的数据
        print('request  post is:',request.POST)
        print('uname is',request.POST['uname'])
        print(request.POST.get('uname_no','no  uname_no'))
        print(request.POST.getlist('uname'))

        return HttpResponse('post  is  ok')

    else:
        pass

    return HttpResponse('test get post is ok')


def test_html(request):
    # from django.template import loader
    # t = loader.get_template('test_html.html')
    # html = t.render()
    # return HttpResponse(html)
    from django.shortcuts import render
    dic = {
        'name':'baiguanghui',
        'age':'27',
    }
    return render(request,'test_html.html',dic)

def test_html_param(request):
    from django.shortcuts import render
    dic = {}
    dic['int'] = 888
    dic['str'] = 'abcd'
    dic['lst'] = ['Tom','Jack','Luck']
    dic['dict'] = {'a':9,'b':8}
    dic['func'] = say_hi
    dic['class_obj'] = Dog()
    dic['script'] = '<script>alert(1111)</script>'
    return  render(request,'test_html_param.html',dic)

def say_hi():
    return 'hahaha'
class Dog:
    def say(self):
        return 'wangwang'
def test_if_for(request):
    from django.shortcuts import render
    dic = {}
    dic['lst'] = ['jack','tom','lili']
    dic['x'] = 12
    return render(request,'test_if_for.html',dic)
def test_mycal(request):
    if request.method == 'GET':
        return render(request,'mycal.html')
    elif request.method == 'POST':
        # 处理计算
        x = int(request.POST['x'])
        y = int(request.POST['y'])
        op = request.POST['op']
        result = 0
        if op == 'add':
            result = x + y
        elif op == 'sub':
            result = x - y
        elif op == 'mul':
            result = x * y
        elif op == 'div':
            result = x / y

        return render(request,'mycal.html',locals())

def base_view(request):
    lst = ['Tom','Jack']
    return render(request,'base.html',locals())
def music_view(request):

    return render(request,'music.html')
def sport_view(request):

    return render(request,'sport.html')
def test_url(request):

    return render(request,'test_url.html')

def test_url_result(request,age):
    from django.urls import reverse
    url = reverse('base_index')

    return HttpResponseRedirect(url)
    # return HttpResponse('---test url result  is ok')
def test_static(request):

    return render(request,'test_static.html')

def set_coookies(request):

    resp = HttpResponse('set cookies is ok')
    resp.set_cookie('uuname','gxn',500)
    return resp

def get_cookies(request):

    value = request.COOKIES.get('uuname')
    return HttpResponse('value is %s'%(value))

def set_session(request):

    request.session['uname'] = 'wwc'
    return HttpResponse('set session is ok')

def get_session(request):

    value = request.session['uname']
    return HttpResponse('session value is %s'%(value))



